/// <reference name="MicrosoftAjax.js" />

//Initialize all classes
Type.registerNamespace("BbqGallery");
//BbqGallery.AsyncPostBackModes indicates which kind of async postback user has submitted
BbqGallery.AsyncPostBackModes = function(){};
BbqGallery.AsyncPostBackModes.prototype = 
{
    None:           0,
    DeleteAlbum:    1,
    CreateAlbum:    2,
    RenameAlbum:    3,
    RenamePicture:  4,
    Pager:          5,
    DeletePicture:  6,
    UploadPicture:  7,
    ImportFromFlickr: 8
}
BbqGallery.AsyncPostBackModes.registerEnum("BbqGallery.AsyncPostBackModes");

var __bbqGallery_ActivatedBgrColor;
//var __bbqGallery_HoverAlbumIncreasing;
var __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.None;
var __bbqGallery_CommonVar;

function __bbqGallery_SetCurrentPage(sender, hiddenField)
{
    if(!hiddenField) return;
    //find the current index
    var index = hiddenField.value;
    if(!index || index.length==0)
        index = 1;
    else
        index = parseInt(index);
    var parent = sender.parentNode.parentNode;
    for(i=0; i<parent.childNodes.length; i++)
    {
        var node = parent.childNodes[i].childNodes[0];
        if(node && node.nodeType==3)    //text node
        {
            if(index==parseInt(node.nodeValue))
                __bbqGallery_SetPagerLink(node, sender);
            break;
        }
    }
    
    hiddenField.value = parseInt(sender.innerHTML);
    __bbqGallery_SetActivatedPagerLink(sender);
    //re-create context menu 
    __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.Pager;
    __bbqGallery_CommonVar = sender.id.substr(0, sender.id.lastIndexOf("_"));
}

function __bbqGallery_SetActivatedPagerLink(sender)
{
    var parent = sender.parentNode; //DIV
    var text = parent.childNodes[0];
    if(text==sender)    //no text node
    {
        text = document.createTextNode(sender.innerHTML);
        parent.insertBefore(text, sender);
    }
    else
        text.style.display = "";
    sender.style.display = "none";
    parent.style.backgroundColor = __bbqGallery_ActivatedBgrColor;
}

//current: text
function __bbqGallery_SetPagerLink(current, sampleLink)
{
    var parent = current.parentNode;
    parent.childNodes[1].style.display = "";    //show link
    parent.removeChild(current);
    __bbqGallery_ActivatedBgrColor = parent.style.backgroundColor;
    parent.style.backgroundColor = sampleLink.parentNode.style.backgroundColor;
}

function __bbqGallery_ItemHover(sender, bgrColor, borderColor, hover)
{
    if(hover)
    {
        sender.oBgr = sender.style.backgroundColor;
        sender.oBorder = sender.style.borderColor;
    }
    sender.style.backgroundColor = hover ? bgrColor : sender.oBgr;
    sender.style.borderColor = hover ? borderColor : sender.oBorder;
}

function __bbqGallery_Show(id)
{
    var obj = $get(id); 
    obj.style.display = obj.oDisplay?obj.oDisplay:'';
}

function __bbqGallery_Hide(id)
{
    var obj = $get(id);
    obj.oDisplay = obj.style.display;
    obj.style.display = 'none';
}

function __bbqGallery_AlbumHover(sender, opacityStart, opacityEnd, fullFading, hover, circle)
{
    if(typeof(sender)=="string")
        sender = document.getElementById(sender);
    var opacity = (sender.opacity) ? sender.opacity : opacityStart;
    var step = (opacityStart < opacityEnd) ? 4 : -4;
        
    var style = sender.style;
    if(hover || fullFading)
    {
        if(style.opacity) style.opacity = (opacity / 100);
        if(style.MozOpacity) style.MozOpacity  = (opacity / 100);
        if(style.KhtmlOpacity) style.KhtmlOpacity  = (opacity / 100);
        if(style.filter) style.filter = "alpha(opacity=" + opacity + ")";
        
        //if fading when mouse over isn't finished, clear it.
        if(!hover)
        {
            if(sender.fadingOver)
            {
                clearInterval(sender.timer);
                sender.opacity = null;
                sender.fadingOver = null;
            }
        }
        else
        {
            sender.fadingOver = true;
        }
        
        if(sender.opacity == opacityEnd)
        {
            clearInterval(sender.timer);
            sender.opacity = null;
            sender.timer = null;
            sender.fadingOver = null;
        }
        else if(circle)
        {
        }
        else 
        {
            if(!sender.opacity)
            {
                sender.opacity = opacity;
                sender.timer = setInterval("__bbqGallery_AlbumHover(\"" + sender.uniqueID + "\", " + opacityStart + ", " + opacityEnd + 
                    ", " + fullFading + ", " + (hover?"true":"false") + ", " + (circle?"true":"false") + ")", 1);
            }
            else
            {
                sender.opacity = opacity + step;
                if((step > 0 && sender.opacity > opacityEnd) || (step < 0 && sender.opacity < opacityEnd))
                    sender.opacity = opacityEnd;
            }
        }
    }
    else
    {
        //Clear timer if it's necessary
        if(sender.timer)
        {
            clearInterval(sender.timer);
            sender.opacity = null;
            sender.timer = null;
            sender.fadingOver = null;
         }
        style.filter = "alpha(opacity=" + opacityEnd + ")";
        opacityEnd = opacityEnd / 100;
        style.opacity = opacityEnd;
        style.MozOpacity  = opacityEnd;
        style.KhtmlOpacity  = opacityEnd;
    }
}

function __bbqGallery_SimulateClick(target)
{
    if(target.click)
        target.click();
    else
    {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        target.dispatchEvent(e);
    }
}

function __bbqGallery_RenameAlbum(beforeSubmit, divEditAreaId, descControlId, sender)
{
    if(beforeSubmit)
    {
        __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.RenameAlbum;
        if(sender)
            __bbqGallery_CommonVar = sender.id.substr(0, sender.id.lastIndexOf("_"));
    }
    else
    {
        if(!divEditAreaId || !descControlId)
        {
            var ownerId = __bbqGallery_RemovePic(__bbq_ownerId);
            divEditAreaId = ownerId + "_divEditArea";
            descControlId = ownerId + "_descControl";
        }
        __bbqGallery_Show(divEditAreaId);
        __bbqGallery_Hide(descControlId);
        return false;
    }
}

function __bbqGallery_CreateAlbum(isNavigator)
{
    var ownerID = __bbqGallery_RemovePic(__bbq_ownerId);
    var prefix = ownerID.substr(0, ownerID.lastIndexOf("_") + 1);
    if(isNavigator)
    {
        __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.CreateAlbum;
    }
    else
    {
        var albumID = $get(ownerID  + "_idAlbum").value;
        var hfCreateAlbum = $get(prefix + "hfCreateAlbum");
        hfCreateAlbum.value = albumID;
    }
       
    __bbqGallery_SimulateClick($get(prefix + "lnkModalNewAlbum"));
    return false;
}

function __bbqGallery_DeleteAlbum()
{
    if(confirm('Do you want to delete this album?'))
    {
        var ownerID = __bbqGallery_RemovePic(__bbq_ownerId);
        var albumID = $get(ownerID + "_idAlbum").value;
        //From context menu id (__bbq_activeId), we find the GalleryAlbumLayoutRenderer.activateAlbumId
        var album = $get(ownerID.substr(0, ownerID.lastIndexOf("_")) + "_activateAlbumId");
        album.value = albumID;
        __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.DeleteAlbum;
    }
    else return false;
}

function __bbqGallery_RenameGalleryItem(beforeSubmit, divEditAreaId, descControlId, sender)
{
    if(beforeSubmit)
    {
        __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.RenamePicture;
        if(sender)
            __bbqGallery_CommonVar = sender.id.substr(0, sender.id.lastIndexOf("_"));
    }
    else
    {
        if(!divEditAreaId || !descControlId)
        {
            var ownerId = __bbqGallery_RemovePic(__bbq_ownerId);
            divEditAreaId = ownerId + "_divEditArea";
            descControlId = ownerId + "_descControl";
        }
        __bbqGallery_Show(divEditAreaId);
        __bbqGallery_Hide(descControlId);
        return false;
    }
}

function __bbqGallery_DeleteGalleryItem()
{
    if(confirm('Do you want to delete this picture'))
    {
        var ownerId = __bbqGallery_RemovePic(__bbq_ownerId);
        var gId = ownerId + "_idItem";
        var galleryItem = $get(gId);
        __bbqGallery_CommonVar = ownerId.substr(0, ownerId.lastIndexOf("_"));
        var hfId = __bbqGallery_CommonVar + "_activateGalleryItemId";
        var hfGalleryItem = $get(hfId);
        hfGalleryItem.value = galleryItem.value;
        
        __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.DeletePicture;
    }
    else return false;
}

function __bbqGallery_UploadPicture()
{
  if(!__bbqGallery_CommonVar)
  {
    var s = __bbqGallery_RemovePic(__bbq_ownerId);
    __bbqGallery_CommonVar = s.substr(0, s.lastIndexOf("_"));
  }
  __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.UploadPicture;
}

function __bbqGallery_ImportFromFlickr()
{
  if(!__bbqGallery_CommonVar)
  {
    var s = __bbqGallery_RemovePic(__bbq_ownerId);
    __bbqGallery_CommonVar = s.substr(0, s.lastIndexOf("_"));
  }
  __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.ImportFromFlickr;
}

function __bbqGallery_DownloadGalleryItem()
{
    var ownerId = __bbqGallery_RemovePic(__bbq_ownerId);
    var gId = ownerId + "_idItem";
    var galleryItem = $get(gId);
    var hfId = ownerId.substr(0, ownerId.lastIndexOf("_")) + "_activateGalleryItemId";
    var hfGalleryItem = $get(hfId);
    hfGalleryItem.value = galleryItem.value;
}

function __bbqGallery_RemovePic(s)
{
    if(s.substr(s.length-3, 3)=="Pic")
        return s.substr(0, s.length-3);
    else
        return s;
}

function __bbqGallery_EndRequestHandler(sender, args)
{
    var ownerID, prefix, cmId, albumId, album;
    if(__bbqGallery_PostBackMode==BbqGallery.AsyncPostBackModes.DeleteAlbum || __bbqGallery_PostBackMode==BbqGallery.AsyncPostBackModes.CreateAlbum)
    {
        ownerID = __bbqGallery_RemovePic(__bbq_ownerId);
        prefix = ownerID.substr(0, ownerID.lastIndexOf("_") + 1);
        cmId = prefix + "_CM";
        var count = 0;
        while(true)
        {
            albumId = prefix + "A" + count.toString() + "Pic";
            album = $get(albumId);
            if(album)
                __bbq_attactCMEvents(albumId, cmId, 500);
            else break;
            count++;
        }
        //Reset create album hidden field
        if(__bbqGallery_PostBackMode==BbqGallery.AsyncPostBackModes.CreateAlbum)
        {
            $get(prefix + "hfCreateAlbum").value = "";
            $get(prefix + "txtAlbum").value = "";
            $get(prefix + "NewAlbumDlg").style.visibility = "hidden";
        }
    }    
    else if(__bbqGallery_PostBackMode==BbqGallery.AsyncPostBackModes.RenameAlbum)
    {
        ownerID = __bbq_ownerId ? __bbqGallery_RemovePic(__bbq_ownerId) : __bbqGallery_CommonVar;  
        prefix = ownerID.substr(0, ownerID.lastIndexOf("_") + 1);
        cmId = prefix + "_CM";
        __bbq_attactCMEvents(ownerID, cmId, 500);
    }
    else if(__bbqGallery_PostBackMode==BbqGallery.AsyncPostBackModes.RenamePicture)
    {
        ownerID = __bbq_ownerId ? __bbqGallery_RemovePic(__bbq_ownerId) : __bbqGallery_CommonVar; 
        prefix = ownerID.substr(0, ownerID.lastIndexOf("_") + 1);
        cmId = prefix + "_GICM";
        __bbq_attactCMEvents(ownerID, cmId, 500);
    }
    else if(__bbqGallery_PostBackMode == BbqGallery.AsyncPostBackModes.Pager || 
            __bbqGallery_PostBackMode == BbqGallery.AsyncPostBackModes.DeletePicture ||
            __bbqGallery_PostBackMode == BbqGallery.AsyncPostBackModes.UploadPicture)
    {
        var i = 0;
        cmId = __bbqGallery_CommonVar + "__GICM";
        while(true)
        {
            albumId = __bbqGallery_CommonVar + "_gi" + i.toString() + "Pic";
            album = $get(albumId);
            if(album)
            {
                if(typeof(__bbq_attactCMEvents)!='undefined')
                    __bbq_attactCMEvents(albumId, cmId, 500);
            }
            else
                break;
            i++;
        }
        //Hide dialog for upload
        if(__bbqGallery_PostBackMode == BbqGallery.AsyncPostBackModes.UploadPicture)
        {
          $get(__bbqGallery_CommonVar + "_UploadPictureDlg").style.visibility = "hidden";
          var upload = $get(__bbqGallery_CommonVar + "_uploadPicture");
          
          var newUpload = upload.cloneNode();
          var parent = upload.parentNode;
          parent.insertBefore(newUpload, upload);
          parent.removeChild(upload);
        }
        else if(__bbqGallery_PostBackMode == BbqGallery.AsyncPostBackModes.ImportFromFlickr)
        {
          $get(__bbqGallery_CommonVar + "_ImportFromFlickrDlg").style.visibility = "hidden";
          var txtTag = $get(__bbqGallery_CommonVar + "_txtTagName");
          txtTag.value = "";
          alert("Importing from Flickr is successful!");
        }
        
        myLightbox.updateImageList();
    }
    __bbqGallery_PostBackMode = BbqGallery.AsyncPostBackModes.None;
}

Sys.Application.add_init(function(){
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(__bbqGallery_EndRequestHandler);
});